clear all
set more off
cd C:\BMOcode\

****************************************************************************************************************************************************************
* Load raw data with shareholder-level ownership variables
****************************************************************************************************************************************************************
use BMO_Pseudodata_main.dta, clear

* Create high and low-denomination groups
qui pctile dispose = denom_ord, n(10) 
gen Dstocksize_low3rd = (denom_ord<=r(r3)) 
gen Dstocksize_top3rd = (denom_ord>=r(r7)) 
drop dispose

keep firm Dstocksize_low3rd Dstocksize_top3rd DR denom_ord
joinby firm using "BMO_Pseudodata_ownership.dta"

*Generate percentage ownership
gen perc_ow=numberofshares/totshares
  
*Generate percentage votes
gen perc_vote=numberofvotes/totvotes

*Generate percentage equity owned by all SHs holding less than 2.5%
bysort firm (perc_ow): gen cum_ow = sum(perc_ow)
by firm: gen ow_bel_025_temp=cum_ow if perc_ow<0.025
by firm: egen all_SH_ow_bel_025=max(ow_bel_025_temp)
drop  cum_ow ow_bel_025_temp

*Generate percentage equity owned by all SH holding more or equal to 2.5%
gen all_SH_ow_above_025=1 - all_SH_ow_bel_025

*Generate total votes held by all shareholders holding less than 2.5%
bysort firm (perc_ow): gen Out_cum_vote_ = sum(perc_vote)
by firm: gen vote_bel_all025_temp=Out_cum_vote_ if perc_ow<0.025
by firm: egen vote_bel_all025=max(vote_bel_all025_temp)
drop   Out_cum_vote_ vote_bel_all025_temp

*Generate total votes held all shareholders holding more or eq. to 2.5%
gen vote_above_all025=1-vote_bel_all025

*Generate value personal holding
gen money_value=numberofshares*denom
label variable money_value "Monetary value of holding" 

*Generate cumulative ownership by company (descending order)
gen neg_perc_ow=-perc_ow
bysort firm (neg_perc_ow) : gen cum_perc_ow = sum(perc_ow)
drop neg_perc_ow


quietly by firm: gen dup = cond(_N==1,0,_n)

*Generate C3: the ow. perc. of the largest 3 blockholders
gen temp_C3= cum_perc_ow if dup==3
by firm: egen C3=max(temp_C3)
label variable C3 "Ow. top 3 SH"
drop temp_C3

 
 *Generate cumulative voting by company (descending order)
*then compute cumulative vote percentages of top 3 owners
gen neg_perc_vt=-perc_vote
bysort firm (neg_perc_vt) : gen cum_perc_vt = sum(perc_vote)
drop neg_perc_vt
sort firm (dup)
 
 *Generate V3: the cumulative vote perc. of the largest 3 blockholders
gen temp_V3= cum_perc_vt if dup==3
by firm: egen V3=max(temp_V3)
label variable V3 "Votes top 3 SH"
drop temp_V3
 
*Generate C5: the ow. perc. of the largest 5 blockholders
gen temp_C5= cum_perc_ow if dup==5
by firm: egen C5=max(temp_C5)
label variable C5 "Ow. top 5 SH"
drop temp_C5

*Generate V5: the cumulative vote perc. of the largest 5 blockholders
gen temp_V5= cum_perc_vt if dup==5
by firm: egen V5=max(temp_V5)
label variable V5 "Votes top 5 SH"
drop temp_V5

*Compute median ownership by firm
bysort firm: egen med_perc_ow=median(perc_ow)
bysort firm: egen med_mon_ow_temp=median(money_value)
gen med_mon_ow=med_mon_ow_temp/1000
drop med_mon_ow_temp
label variable med_perc_ow "Median ow."
label variable med_mon_ow "Median monetary ow."
drop money_value

*Compute number of shareholders per firm
egen num_SH=sum(shareholderid>0), by(firm)
label variable num_SH "SH number"

*Compute insider (managers) ownership 
gen ow_ind_ins=perc_ow*insiderind
bysort firm: gen temp_tot_ins_ow = sum(ow_ind_ins)
by firm: egen tot_ins_ow=max(temp_tot_ins_ow)
label variable tot_ins_ow "Inside ow."
drop ow_ind_ins
drop temp_tot_ins_ow


*Generate COlarge (ratio between voting and CF rights for the largest shareholder)
by firm: egen maxCF=max(perc_ow)
by firm: egen maxvote=max(perc_vote)
gen COlarge= maxvote/maxCF
label variable COlarge "Vote/CF largest SH"    
drop maxCF maxvote

*Generate COsmall (ratio between voting and CF rights for the smallest shareholder)
by firm: egen minCF=min(perc_ow)
by firm: egen minvote=min(perc_vote)

* In firm 54 and 49: smallest shareholder owns a half-share w/o voting rights. Replace with smallest shareholder who has voting rights. 
replace minCF=0.0025 if firm==54
replace minvote=0.0172414 if firm==54
replace minCF=0.0005 if firm==49
replace minvote=0.0006901 if firm==49

gen COsmall= minvote/minCF
drop minCF minvote 
label variable COsmal "Vote/CF smallest SH"

*Generate COmedian (ratio between voting and CF rights for the median shareholder)
*Note: for the percentage ownership we do not use the previously computed median percentage ownership
*to account for the possibility that the median ownership computed via the stata function "median"
*is not an observed ownership stake but the mid-point of two ownership stakes
by firm: egen medianCF_temp=median(perc_ow)
gen diffmed_temp=abs(medianCF_temp-perc_ow)
by firm: egen medianCF_temp2=min(diffmed_temp)
gen medianCF=medianCF_temp2+medianCF_temp
gen medvote=perc_vote if  perc_ow==medianCF
gen COmedian_temp= medvote/medianCF
by firm: egen COmedian=max(COmedian_temp)
drop   medianCF_temp diffmed_temp medianCF_temp2 medianCF medvote COmedian_temp
label variable COmedian "Vote/CF median SH"    

*Generate Herfindahl index
gen pct_ow_sq=perc_ow^2
egen Herf=sum(pct_ow_sq), by(firm)
label variable Herf "Herfindahl Ind."

*Generate largest block % and value
egen largestBlock_pct=max(perc_ow), by(firm)
gen large_block_value=largestBlock_pct*totshares*denom/1000
label variable largestBlock_pct "Largest block"
label variable large_block_value "Monetary value largest block"


*Generate smalleest block % and value
egen smallestBlock_pct=min(perc_ow), by(firm)
gen small_block_value=smallestBlock_pct*totshares*denom/1000
label variable smallestBlock_pct "Smallest block" 
label variable small_block_value "Monetary value smallest block"

*Clean up
drop numberofshares insiderind numberofvotes perc_ow perc_vote cum_perc_ow cum_perc_vt pct_ow_sq shareholderid
drop if dup>1
drop dup  


* Set mid-denomination group to missing (for t-tests)
gen SDhelp = (Dstocksize_low3rd==1)
gen LDhelp = (Dstocksize_top3rd==1)
gen SD=SDhelp
replace SD = . if SDh==0 & LDh==0 
gen LD=LDhelp
replace LD = . if SDh==0 & LDh==0 
drop SDhelp LDhelp 


*** Table 3: Ownership structure in the subsample
do BMO_table3.do

*** Table 4: Shareholder blocks in the subsample
do BMO_table4.do

*** Table 6: Voting rights in the subsample
do BMO_table6.do



